package com.lishui.waterojbackenduserservice.controller;

import com.lishui.waterojbackendmodel.model.annotation.AuthCheck;
import com.lishui.waterojbackendmodel.model.annotation.JudgeEmpty;
import com.lishui.waterojbackendcommon.contant.MessageConstant;
import com.lishui.waterojbackendcommon.contant.UserConstant;
import com.lishui.waterojbackendcommon.exception.BusinessException;
import com.lishui.waterojbackendcommon.exception.ParameterException;
import com.lishui.waterojbackendcommon.result.Result;
import com.lishui.waterojbackendmodel.model.enumeration.OperationTypeEnum;
import com.lishui.waterojbackendmodel.model.pojo.dto.UserLoginDTO;
import com.lishui.waterojbackendmodel.model.pojo.dto.UserRegisterDTO;
import com.lishui.waterojbackendmodel.model.pojo.dto.UserUpdateDTO;
import com.lishui.waterojbackendmodel.model.pojo.vo.UserVO;
import com.lishui.waterojbackenduserservice.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


/**
 * 用户接口

 */
@RestController
@RequestMapping
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 用户注册
     *
     * @param userRegisterDTO
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    @JudgeEmpty(OperationTypeEnum.YES)
    public Result<Long> userRegister(@Valid @RequestBody UserRegisterDTO userRegisterDTO) {
        long result = userService.userRegister(userRegisterDTO);
        return Result.success(result);
    }

    /**
     * 用户登录
     *
     * @param userLoginDTO
     * @param request
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    @JudgeEmpty(OperationTypeEnum.YES)
    public Result<UserVO> userLogin(@Valid @RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request) {
        UserVO userVO = userService.userLogin(userLoginDTO, request);
        return Result.success(userVO);
    }

    /**
     * 用户退出登录
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("用户退出登录")
    public Result userLogout(HttpServletRequest request) {
        userService.userLogout(request);
        return Result.success();
    }

    /**
     * 获取当前用户
     * @return
     */
    @GetMapping("/current")
    @ApiOperation("获取当前用户")
    public Result<UserVO> getCurrentUser(HttpServletRequest request) {
        UserVO currentUser = userService.getCurrentUser(request);
        return Result.success(currentUser);
    }

    /**
     * 查询用户列表
     * @param username
     * @return
     */
    @GetMapping
    @ApiOperation("管理员查询用户列表")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public Result<List<UserVO>> searchUsers(String username) {
        List<UserVO> userVOList = userService.searchUsers(username);
        return Result.success(userVOList);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("管理员根据Id删除用户")
    @JudgeEmpty(OperationTypeEnum.YES)
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public Result deleteUser(@PathVariable Long id) {
        if (id <= 0) {
            throw new ParameterException(MessageConstant.USER_ID_EXCEPTION);
        }
        boolean flag = userService.removeById(id);
        if (!flag) throw new BusinessException(MessageConstant.DELETE_USER_FAILED);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改用户信息")
    @JudgeEmpty(OperationTypeEnum.YES)
    public Result update(@Valid @RequestBody UserUpdateDTO userUpdateDTO, HttpServletRequest httpServletRequest) {
        userService.update(userUpdateDTO, httpServletRequest);
        return Result.success();
    }

}
